<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'core/app/DataToValueModel.php';
require_once 'eleontev/Util/TreeHelper.php';
require_once 'eleontev/SQL/TableSortOrder.php';
require_once 'core/common/CommonCategoryModel.php';


class KBCategoryModel extends CommonCategoryModel
{
	var $tbl_pref_custom = 'kb_';
	var $tables = array('table'=>'category', 'category', 'entry', 'entry_to_category');
	var $custom_tables = array('data_to_user_rule', 'data_to_user_value', 'user', 'role');
	
	
	function KBCategoryModel() {
		parent::CommonCategoryModel();
		$this->dv_manager = &new DataToValueModel();
	}
	
	
	// CATEGORY TO USER
	
	function getAdminUserById($record_id) {
			return $this->dv_manager->getDataById($record_id, 
		                                   'kb_category_to_admin', 
		                                   "{$this->tbl->user} u",
										   'u.id',
										   'u.id, CONCAT(u.first_name, " ", u.last_name)');
	}
	
	
	function saveAdminUserToCategory($obj, $record_id) {
		$this->dv_manager->saveData($obj->getAdminUser(), $record_id, 'kb_category_to_admin');
	}
	
	
	function deleteAdminUserToCategory($record_id) {
		$this->dv_manager->deleteData($record_id, 'kb_category_to_admin');
	}
	
	// used when wrong submit to generate list 
	function getAdminUserByIds($ids) {
		$sql = "SELECT u.id, CONCAT(u.first_name, ' ', u.last_name) 
		FROM {$this->tbl->user} u
		WHERE u.id IN ($ids)";
			
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->GetAssoc();	
	}
	
	
	// CATEGORY TO MEMBER ROLE // -----
	
	function getMemberRoleById($record_id, $list_view = false) {

			$select = ($list_view) ? 'dv.data_value AS k, r.title AS v' : 'r.id, r.id AS id1';
			return $this->dv_manager->getDataById($record_id, 
		                                   		 'kb_category_to_member_role', 
		                                         "{$this->tbl->role} r", 
										         'r.id',
												 $select,
												 $list_view);
	}
	
	
	function saveMemberRoleToCategory($obj, $record_id) {
		$this->dv_manager->saveData($obj->getMemberRole(), $record_id, 'kb_category_to_member_role');
	}
	
	
	function deleteMemberRoleToCategory($record_id) {
		$this->dv_manager->deleteData($record_id, 'kb_category_to_member_role');
	}
	

	// ACTIONS // ---------------------
}
?>